package com.yf.response;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;

/**
 * @author chentianwei
 * @date 2023/4/18 17:01
 * @description 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseBody<?>> handleException(Exception e,
                                                           HandlerMethod method){

        ResponseBody<Object> body = ResponseBody.error(e);
        log.error("方法请求异常:" + method.getMethod().getName());
        log.error(e.getMessage(),e);
        return ResponseEntity.status(530).body(body);
    }

    @ExceptionHandler(Error.class)
    public ResponseEntity<ResponseBody<?>> handleError(Error e,
                                                           HandlerMethod method){

        ResponseBody<Object> body = ResponseBody.error(e);
        log.error("方法请求异常:" + method.getMethod().getName());
        log.error(e.getMessage(),e);
        return ResponseEntity.status(530).body(body);
    }


}
